﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class UnitBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public UnitBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(int Type, string Name)
        {
            if (DAO.T_UNITs.Where(t => t.Type == Type && t.NAME.ToLower() == Name.ToLower()).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_UNIT> GetUnitList()
        {
            var list = DAO.T_UNITs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }
            else
            {
                list = list.OrderBy(t=>t.NAME);
            }
            return list;
        }

        public T_UNIT GetUnitByID(string ID)
        {
            return DAO.T_UNITs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }

        public int AddUnit(T_UNIT Unit)
        {
            try
            {
                Unit.ID = DAO.GetGUID();

                DAO.T_UNITs.InsertOnSubmit(Unit);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddUnit(T_UNIT Unit, out string UnitGuid)
        {
            try
            {
                Unit.ID = DAO.GetGUID();
                
                DAO.T_UNITs.InsertOnSubmit(Unit);
                DAO.SubmitChanges();

                UnitGuid = Unit.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                UnitGuid = "";
                return -1;
            }
        }

        public int UpdateUnit(T_UNIT Unit)
        {
            try
            {
                T_UNIT updateEntity = DAO.T_UNITs.SingleOrDefault(t=>t.ID.ToString()==Unit.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.NAME = Unit.NAME;
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        
        public int DeleteUnit(string ID)
        {
            try
            {
                T_UNIT deleteEntity = DAO.T_UNITs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_UNITs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
